// JavaScript Document

//创建ajax对象
var xmlHttp = false;
if(window.ActiveXObject){
	try{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){}
	}
}else if(window.XMLHttpRequest){
	xmlHttp = new XMLHttpRequest();
	if(xmlHttp.overrideMimeType){
		xmlHttp.overrideMimeType('text/xml');
	}
}

/*--------------------------------------------- 实时轨迹 ------------------------------------------------*/

var isClearSetTimeOut = true;  //
var RealTimeSetTimeout; //settimeoutID


var RealTimeSetTimeout1; //settimeoutID



var QueryInfoArr = [];	//用来存放车辆或警员信息的数组
var lastPoint = ''; 	//每次查询的最后一个坐标，一遍与下一次查询的第一个坐标画线
var Time = ''; 			//查询坐标时的时间，点击查询的时候为当前时间，settimeout的时候为上一次查的最后一个坐标时间
var TimeOut = 0; 		//判断是否关机，如果查询3次未查到最近位置就判断为已经关机

function Realtime(){
	//结束实时轨迹的settimeout
	isClearSetTimeOut = true;
	if(RealTimeSetTimeout) clearTimeout(RealTimeSetTimeout);	//清除settimeout
	if(RealTimeSetTimeout1) clearTimeout(RealTimeSetTimeout1);
	
	mapObj.removeAllOverlays();			//清除轨迹及标注
	//弹窗
	var temp='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">查询指定人员或车辆的当前位置并实时监控：</td><tr><td>查询方式：<input type="radio" name="method" value="按条件查" onclick="QueryMethod(this.value)" checked="checked" /> 按条件查 <input name="method" type="radio" value="直接输入" onclick="QueryMethod(this.value)" /> 直接输入</td></tr> <tr><td id="queryMethod">';
	
	
	
	temp+='<div id="method1"  style="position:relative;">选择条件：<select id="area"  style="width:60px;top:0px;" onchange="unitsOption(this.value);this.nextSibling.value=this.value" style="color:#666"><option value="选择区">选择区</select><input type="text" value="选择区" id="input_1" onKeyUp="text_keyup(\'div_1\',this)" style="z-index:10000;width:42px;position:absolute;top:3px;left:60px;"><div style="display:block; top:28px; left:60px;position:absolute; z-index:10000;background:#06F;" id="div_1"></div>';
	
	
	
	
	
	temp+='<select id="units"  onchange="querywordsOption(this.value);this.nextSibling.value=this.value" style="color:#666;width:120px"><option value="选择单位">选择单位</select><input type="text" value="选择单位" id="input_2" onKeyUp="text_keyup_1(\'div_2\',this)" style="z-index:10000;width:102px;position:absolute;top:3px;left:118px;"><div style="display:block; top:28px; left:118px;position:absolute; z-index:10000;background:#06F;" id="div_2"></div>';
	
	
	
	temp+='<select id="type" onchange="typeOption(this.value)" style="color:#666"><option value="车辆">车辆</option><option value="摩托">摩托</option><option value="人员">人员</option></select><br />';
	temp+='<label id="byWhat1">选择车牌</label>：<select id="querywords1" style="width:184px; color:#666" disabled="disabled"><option value="unselected">选择车辆</select></div><div id="method2" style="display:none">选择条件：<input type="radio" id="ByPlatenum" name="By" value="车牌" onclick="RealTimeBy(\'输入车牌\')" checked="checked" /> 车牌号 <input type="radio" id="ByManNum" name="By" value="人员号" onclick="RealTimeBy(\'输入人员号\')" /> 人员号<br /><label id="byWhat2">输入车牌</label>：<input type="text" id="querywords2" name="querywords2" value="豫A" style="width:180px; color:#666" /></div></td></tr><tr><td align="right"><input name="" type="button" class="btnbg" value="实时监控" style="width:70px;" onclick="javascript:QueryInfoAjax()"/><input type="hidden" id="CallKey" /></td></tr><tr><td align="left"><div id="RealTimeNotice" style="color:red"><font color="green">显示<font color="blue">最近时间内在线</font>设备,每次查看前点击"实时轨迹"菜单更新。<br />如网络速度太慢，浏览器会锁定无法操作，可尝试多点几次。</font></div><div id="RealTimeInfo"></div><div id="RealtimeMsm" style="display:none"><textarea id="msgContent" style="width:100%; height:50px"></textarea><br /><input name="" type="button" class="btnbg" value="发送短信" style="width:70px;" onclick="javascript:sendMessage(document.getElementById(\'phonenum\').innerHTML)"/> <input name="" id="Call" type="button" class="btnbg" value="语音通话" style="width:70px;" onclick="javascript:realtimeCall()"/> <span id="CallError" style="color:red"></span></div></td></tr></table>';
	showMapMenu("实时轨迹",temp);
	//查询所有区，派出所，警车，警员用于选择
	xmlHttp.open('POST', 'public/Location/ajax.php', false);
	xmlHttp.onreadystatechange = showSelect;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=QuerySelectOption&query=online&t='+Math.random());
}



/*-----------------------------------------------------------------------------------------------------------------------------*/





function text_keyup_1(div,obj){
if(obj.previousSibling.options.length==1){
	obj.value="选择单位";
	return 0;
	}
if(obj.value==""){
		return 0;
		}

var n=0;
	for(var i=1; i<document.getElementById('units').options.length;i++){
	   if(document.getElementById('units').options[i].text==obj.value){
		   document.getElementById('units').options[i].selected=true;
		   querywordsOption(obj.value)
		   n=1;
		   }
	}
	if(n==1){
		return 0;
		}

	$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"a="+document.getElementById('area').value+"&text="+obj.value,
			success:function(data){
		         if(data!=""&&data.split("_").length>0)
			     {
				 out_list(data);
				 }
				 else{
				document.getElementById(div).innerHTML="";
				}
			}
		});
	function out_list(value){
	var out="<table width='135px' style\"border-width:1px; border-color:#4c62ff; border-style:solid; \">";
	out+="<tr><td style=\"height:0px\"></td></tr>";
	for(var i=0;i<value.split("_").length;++i){
	out=out+"<tr><td onclick=\"sub_1(this.title,'input_2','div_2')\" title='"+value.split("_")[i]+"' style=\"cursor:pointer;\" onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\">"+value.split("_")[i]+"</td></tr>";
	}
	out=out+"<tr><td onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\" style=\"text-align:right;cursor:pointer;\" onclick=\"close_div('div_2')\">关闭</td></tr>";
	out=out+"</table>";
	obj1=document.getElementById("div_2");
	obj1.innerHTML=out;
		}
		
	}
function text_keyup(div,obj){
	var n=0;
	for(var i=0; i<jsonObj.length;i++){
		if(jsonObj[i].AREA ==obj.value){
			document.getElementById('area').options[i].selected=true;
			if(document.getElementById('units').options.length = 1){
				var unitsObj = jsonObj[i].INFO
				for( var j=0;j<unitsObj.length;j++){
					var oOption = document.createElement('OPTION');
					oOption.text = unitsObj[j].DEPARTMENT;
					oOption.value = unitsObj[j].DEPARTMENT;
					document.getElementById('units').options.add(oOption); 
				}
			}
		n=1;
		}
	}
	if(n==1){
		return 0;
		}
	if(obj.value==""){
		return 0;
		}
			$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"text="+obj.value,
			success:function(data){
		         if(data!=""&&data.split("_").length>0)
			     {
				 out_list(data);
				 }
				 else{
				document.getElementById(div).innerHTML="";
				}
			}
		});
	

	function out_list(value){
		var out="<table width='70px' style\"border-width:1px; border-color:#4c62ff; border-style:solid; \">";
		out+="<tr><td style=\"height:0px\"></td></tr>";
	for(var i=0;i<value.split("_").length;++i){
	out=out+"<tr><td onclick=\"sub_1(this.title,'input_1','div_1')\" title='"+value.split("_")[i]+"' style=\"cursor:pointer;\" onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\">"+value.split("_")[i]+"</td></tr>";
	}
	out=out+"<tr><td onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\" style=\"text-align:right;cursor:pointer;\" onclick=\"close_div('div_1')\">关闭</td></tr>";
	out=out+"</table>";
	obj1=document.getElementById("div_1");
	obj1.innerHTML=out;
		}
	//document.getElementById(div).innerHTML="sdfsdf";
	}
function sub_1(text,a,b){
	document.getElementById(a).value=text;
		if(b=='div_2'){
	for(var i=1; i<document.getElementById('units').options.length;i++){
	   if(document.getElementById('units').options[i].text==text){
		   document.getElementById('units').options[i].selected=true;
		   querywordsOption(text)
		   }
	}
		}else{
	
	for(var i=0; i<jsonObj.length;i++){
		if(jsonObj[i].AREA == text){
			document.getElementById('area').options[i+1].selected=true;
			if(document.getElementById('units').options.length = 1){
				var unitsObj = jsonObj[i].INFO
				for( var j=0;j<unitsObj.length;j++){
					var oOption = document.createElement('OPTION');
					oOption.text = unitsObj[j].DEPARTMENT;
					oOption.value = unitsObj[j].DEPARTMENT;
					document.getElementById('units').options.add(oOption); 
				}
			}
		}
	}
			unitsOption(text);
	}
	close_div(b);
	}
	
function close_div(a){
	var obj=document.getElementById(a);
	obj.innerHTML="";
	}
	
	
	
	
/*-----------------------------------------------------------------------------------------------------------------------------*/
	
	
//选择输入车牌号或者输入警号
function RealTimeBy(byWhat){
	if(byWhat == '输入车牌'){//判断显示车牌还是警号
		$('#byWhat2').html('输入车牌');
		$('#querywords2').val('豫A');//input框的初始值
	}else{
		$('#byWhat2').html('输入人员');
		$('#querywords2').val('');//input框的初始值
	}
}


//实时路线ajax调用php文件
function QueryInfoAjax(){
	if(!xmlHttp){
		alert('不能创建XMLHttpRequest对象实例');
		return false;
	}
	//判断按钮的作用是开始还是停止
	if(isClearSetTimeOut == true){
		isClearSetTimeOut = false;
	}
	else if(isClearSetTimeOut == false){
		isClearSetTimeOut = true;
		clearTimeout(RealTimeSetTimeout);	//清除settimeout
		mapObj.removeAllOverlays();			//清除轨迹及标注
		$('#RealTimeInfo').html('');		//清空信息
		//再次初始化
		QueryInfoArr = [];	//用来存放车辆或警员信息的数组
		lastPoint = ''; 	//每次查询的最后一个坐标，一遍与下一次查询的第一个坐标画线
		Time = ''; 			//查询坐标时的时间，点击查询的时候为当前时间，settimeout的时候为上一次查的最后一个坐标时间
		TimeOut = 0; 		//判断是否关机，如果查询3次未查到最近位置就判断为已经关机
		isClearSetTimeOut = false;
	}
	//判断选择的方式 metho = true 按条件查找 false 直接输入
	var method = document.getElementsByName('method').item(0).checked;
	if(method){
		var byWhat = $('#byWhat1').html() == '选择车牌'?'plate':'sn'; //判断查询警车或警员
		var QueryWords = document.getElementById('querywords1').value;
		//如果是警员，需要截取警号部分
		if(byWhat == 'sn'){
			var QueryWords = QueryWords.split(' - ')[0];
		}
		//判断是否选择了车辆或警员
		if(QueryWords == 'unselected'){
			$('#RealTimeNotice').html('请根据条件'+$('#byWhat1').html());
			return false;
		}
	}else{
		var byWhat = $('#byWhat2').html() == '输入车牌'?'plate':'sn'; //判断查询警车或警员
		//获取是否车牌号或者警员编号并判断不为空
		var QueryWords = $('#querywords2').attr("value");
		if(byWhat == 'plate'){ //判断车牌不能为空
			if(QueryWords == '' || QueryWords == '豫A'){
				$('#RealTimeNotice').html('请输入车牌号，可以为车辆或摩托车');
				return false;
			}
		}else{ //判断警号不能为空
			if(QueryWords == ''){
				$('#RealTimeNotice').html('请输入人员编号');
				return false;
			}
		}
		
	}
	$("#RealtimeMsm").css("display","none"); //点击查询时隐藏已经显示的打电话发短信
	$('#RealTimeNotice').html('');//如果不为空则清空提示信息
	//用post方式向php传值
	xmlHttp.open('POST', 'public/Location/ajax.php', true);
	xmlHttp.onreadystatechange = QueryInfoAjaxResult;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=QueryInfo&byWhat='+byWhat+'&QueryWords='+QueryWords+'&t='+Math.random());
}

function QueryInfoAjaxResult(){
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//获取php查询的结果字符串,并判断是否为空
			var str = xmlHttp.responseText;
			//根据返回信息判断查询的信息是否存在
			if(str == 'VehicleQueryNone'){
				$('#RealTimeNotice').html('没有找到该车牌号的信息，请检查输入是否正确');
			}else if(str == 'PersonQueryNone'){
				$('#RealTimeNotice').html('没有找到该人员的信息，请检查输入是否正确');
			}else{//信息存在，根据返回的信息再次调用ajax.php查询位置信息
				$('#RealTimeNotice').html('已经找到信息，请等待查询位置信息……');
				//将字符串分割成数组放到一个全局数组中
				for(var i=0;i<str.split('|').length;i++){
					var arr = str.split('|')[i].split(':');
					QueryInfoArr[arr[0]] = arr[1];
				}
				//判断显示信息
				if(QueryInfoArr['type'] == '车辆'){
					var infoStr = '车牌号: '+QueryInfoArr['plate']+' <span id="speed_now" style="color:red"></span><br />所属单位: '+QueryInfoArr['departmentName']+'<br />车辆别名: '+QueryInfoArr['name']+'<br />车辆类型: '+QueryInfoArr['type']+'<br />绑定手机: <span id="phonenum">'+QueryInfoArr['sim']+'</span>';
				}else if(QueryInfoArr['type'] == '摩托'){
					var infoStr = '车牌号: '+QueryInfoArr['plate']+' <span id="speed_now" style="color:red"></span><br />所属单位: '+QueryInfoArr['departmentName']+'<br />车辆别名: '+QueryInfoArr['name']+'<br />车辆类型: '+QueryInfoArr['type']+'<br />绑定手机: <span id="phonenum">'+QueryInfoArr['sim']+'</span>';
				}else{
					var infoStr = '人员姓名: '+QueryInfoArr['name']+' <span id="speed_now" style="color:red"></span><br />所属单位: '+QueryInfoArr['departmentName']+'<br />人员编号: '+QueryInfoArr['sn']+'<br />人员性别: '+(QueryInfoArr['gender'] == '1'?'男':'女')+'<br />绑定手机: <span id="phonenum">'+QueryInfoArr['sim']+'</span>';
				}
				$('#RealTimeInfo').html(infoStr);
				Time = TimeNow();  //当前时间
				RealTimeAjax();
			}
		}
	}
}

//实时路线ajax
function RealTimeAjax(){
	xmlHttp.open('POST', 'public/Location/ajax.php', true);
	xmlHttp.onreadystatechange = RealTimeAjaxResult;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=RealTime&locTable='+QueryInfoArr['locTable']+'&device='+QueryInfoArr['device']+'&time='+Time+'&t='+Math.random());
}

//实时路线ajax返回的结果
function RealTimeAjaxResult(){
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//获取php查询的结果字符串,并判断是否为空
			var str = xmlHttp.responseText;
			if(typeof(str) !== 'undefined' && str.length>0){
				$('#RealTimeNotice').html('已查询到最新坐标信息，正在初始化，请稍后……'); //如果查到坐标则清空所有提示信息
				var arr = str.split('|');
				var PointArr = [];
				for(var i=0;i<arr.length;i++){
					var objArr = arr[i].split(',');
					PointArr[i] = {lng:objArr[0], lat:objArr[1], time:objArr[2], speed:objArr[3]};
				}
				//如果在线就画出轨迹并显示发送语音或短信
				drawRealLine(PointArr);
				$("#RealtimeMsm").css("display","block");
				
			}else{//在未查找到最新坐标的时候开始判断，如果5次均为查到，就提示设备已经关机				
				$("#RealtimeMsm").css("display","none"); //如果不在线就隐藏发送语音或短信
				$('#speed_now').html(''); //查不到坐标隐藏速度
				
				TimeOut = TimeOut+1;
				$('#RealTimeNotice').html('正在查询最新位置（第'+TimeOut+'次查询）……');
				if(TimeOut > 5){
					$('#RealTimeNotice').html((TimeOut-1)+'次查询均未找到最新坐标，可能设备已经关机或断网。');
					TimeOut = 0;
					return false;
				}
			}
			//每5秒重复查询数据库
			RealTimeSetTimeout = window.setTimeout(function(){
				Time = str == '' || typeof(str) == 'undefined'?TimeNow():PointArr[0].time;//如果查到坐标，下次查就是大于等于最后一个点的时间，否则就查大于等于当前时间的
				RealTimeAjax();
			}, 5000);
		}
	}
}

//根据数据库查询结果画线
function drawRealLine(arr){
	if(lastPoint !== ''){
		arr.unshift(lastPoint);
		$('#RealTimeNotice').html('');
	}
	if(arr[arr.length-1] == lastPoint){
		return false;
	}
	for(var i=0;i<arr.length-1;i++){
		//在车牌的后边写出当前速度
		var speed = arr[i].speed;
		$('#speed_now').html('<font color="green">(速度 '+(Number(speed)*1.842).toFixed(2)+' km/h)</font>');
		var Point1 = [arr[i].lng, arr[i].lat];
		var Point2 = [arr[i+1].lng, arr[i+1].lat];
		var PointArr = [Point1,Point2];
		pantoPoint(arr[arr.length-1].lng,arr[arr.length-1].lat);
		//pantoPoint(arr[arr.length-1].lng,arr[arr.length-1].lat);
		//判断显示信息
		if(QueryInfoArr['type'] == '车辆'){
			var ImgUrl = 'images/ico_car.gif';
			var rotation = PointPointC(arr[i].lng, arr[i].lat, arr[i+1].lng, arr[i+1].lat);
		}else if(QueryInfoArr['type'] == '摩托'){
			var ImgUrl = 'images/ico_moto.gif';
			var rotation = 0;
		}else{
			var ImgUrl = 'images/ico_man.gif';
			var rotation = 0;
		}
		//添加实时运动的标注
		RealTimeMarker('RealTimeMarker', arr[i+1].lng, arr[i+1].lat, false, ImgUrl, rotation)
		//添加线
		addMPolyline(PointArr);
	}
	lastPoint = arr[arr.length-1];
}

function TimeNow(){
	function getLocTime(nS) {
    	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, ""); 
	}
	//获取当前时间的前10秒
	var date = parseInt(new Date().valueOf()/1000)-5;
	return getLocTime(date);
}

//实时轨迹时打电话
function realtimeCall(){
	var Status = document.getElementById('Call');
	var phonenum = document.getElementById('phonenum').innerHTML;
	if(phonenum == ''){
		$('#CallError').html('没有绑定的手机号');
		return false;
	}
	if(Status.value == '语音通话'){
		$.ajax({
			type:"POST",
			url:"public/talk.php",
			cache:false,
			data:"act=convokeMeeting&phoneNo="+phonenum,
			success:function(data){
				var re_arr = data.split(',');
				if(re_arr[0] == '100'){ //呼叫成功
					Status.value = '挂断通话'; //按钮变为挂断
					$('#CallError').html('正在呼叫……');
					document.getElementById('CallKey').value = re_arr[1]; // 将通话的唯一标示赋给赋给隐藏按钮
				}else if(re_arr[0] == '101'){
					$('#CallError').html('101 IP地址有误');
					return false;
				}else if(re_arr[0] == '102'){
					$('#CallError').html('102 其他错误');
					return false;
				}else{
					$('#CallError').html('调用webserverice错误');
					return false;
				}
			}
		});
	}else{
		var CallKey = document.getElementById('CallKey').value // 将通话的唯一标示
		$.ajax({
			type:"POST",
			url:"public/talk.php",
			cache:false,
			data:"act=endMeeting&number="+CallKey,
			success:function(data){
				if(data == '100'){ //挂断成功
					Status.value = '语音通话';
					document.getElementById('CallKey').value = ''; // 将通话的唯一标示删除
					$('#CallError').html('挂断成功');
				}
			}
		});
	}
}

/*--------------------------------------------- 历史轨迹 ------------------------------------------------*/
var JsonLocArr = [];
var jsonObj = '';

function History(){
	//结束实时轨迹的settimeout
	isClearSetTimeOut = true;
	if(RealTimeSetTimeout) clearTimeout(RealTimeSetTimeout);
	if(RealTimeSetTimeout1) clearTimeout(RealTimeSetTimeout1);	//清除settimeout
	mapObj.removeAllOverlays();			//清除轨迹及标注
	var temp='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">查询指定人员或车辆在指定时间段内的路线：</td></tr><tr><td>查询方式：<input type="radio" name="method" value="按条件查" onclick="QueryMethod(this.value)" checked="checked" /> 按条件查 <input name="method" type="radio" value="直接输入" onclick="QueryMethod(this.value)" /> 直接输入</td></tr> <tr><td id="queryMethod"><div id="method1" style="position:relative;">选择条件：';
	//temp+='<select id="area" onchange="unitsOption(this.value)" style="color:#666"><option value="选择区">选择区</select>';
	//temp+='<select id="units" onchange="querywordsOption(this.value)" style="color:#666;width:120px"><option value="选择单位">选择单位</select>';
	
	
	
	temp+='<select id="area"  style="width:60px;top:0px;" onchange="unitsOption(this.value);this.nextSibling.value=this.value" style="color:#666"><option value="选择区">选择区</select><input type="text" value="选择区" id="input_1" onKeyUp="text_keyup(\'div_1\',this)" style="z-index:10000;width:42px;position:absolute;top:3px;left:60px;"><div style="display:block; top:28px; left:60px;position:absolute; z-index:10000;background:#06F;" id="div_1"></div>';
	
	
	
	temp+='<select id="units"  onchange="querywordsOption(this.value);this.nextSibling.value=this.value" style="color:#666;width:120px"><option value="选择单位">选择单位</select><input type="text" value="选择单位" id="input_2" onKeyUp="text_keyup_1(\'div_2\',this)" style="z-index:10000;width:102px;position:absolute;top:3px;left:118px;"><div style="display:block; top:28px; left:118px;position:absolute; z-index:10000;background:#06F;" id="div_2"></div>';
	
	
	
	temp+='<select id="type" onchange="typeOption(this.value)" style="color:#666"><option value="车辆">车辆</option><option value="摩托">摩托</option><option value="人员">人员</option></select><br /><label id="byWhat1">选择车牌</label>：<select id="querywords1" style="width:184px; color:#666" disabled="disabled"><option value="unselected">选择车辆</select></div><div id="method2" style="display:none">选择条件：<input type="radio" id="ByPlatenum" name="By" value="车牌" onclick="HistoryBy(\'输入车牌\')" checked="checked" /> 车牌号 <input type="radio" id="ByManNum" name="By" value="人员号" onclick="HistoryBy(\'输入人员号\')" /> 人员号<br /><label id="byWhat2">输入车牌</label>：<input type="text" id="querywords2" name="querywords2" value="豫A" style="width:180px; color:#666" /></div></td></tr><tr><td>开始时间：<input id="StartTime" type="text" name="StartTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd HH:mm:ss\'})" style="width:184px" /></td></tr><tr><td>结束时间：<input type="text" id="EndTime" name="EndTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd HH:mm:ss\'})" style="width:184px" /></td></tr><tr><td align="right"><input name="" type="button" class="btnbg" value="查询轨迹" style="width:70px;" onclick="QueryHistoryAjax()"/></td></tr><tr><td align="left"><div id="HistoryNotice" style="color:red"></div><div id="HistoryInfo"></div></td></tr></table>';
	showMapMenu("历史轨迹",temp);
	//查询所有区，派出所，警车，警员用于选择
	xmlHttp.open('POST', 'public/Location/ajax.php', false);
	xmlHttp.onreadystatechange = showSelect;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=QuerySelectOption&query=all&t='+Math.random());
}

//选择输入车牌号或者输入警号
function HistoryBy(byWhat){
	if(byWhat == '输入车牌'){				//判断显示车牌还是警号
		$('#byWhat2').html('输入车牌');
		$('#querywords2').val('豫A');		//input框的初始值
	}else{
		$('#byWhat2').html('输入人员号');
		$('#querywords2').val('');		//input框的初始值
	}
	$('#StartTime').val('');
	$('#EndTime').val('');
}

//查询历史轨迹
function QueryHistoryAjax(){
	if(!xmlHttp){
		alert('不能创建XMLHttpRequest对象实例');
		return false;
	}
	$('#HistoryInfo').html(''); //清空上次查询的车辆或警员信息
	//判断选择的方式 metho = true 按条件查找 false 直接输入
	var method = document.getElementsByName('method').item(0).checked;
	if(method){
		var byWhat = $('#byWhat1').html() == '选择车牌'?'plate':'sn'; //判断查询警车或警员
		var QueryWords = document.getElementById('querywords1').value;
		//如果是警员，需要截取警号部分
		if(byWhat == 'sn'){
			var QueryWords = QueryWords.split(' - ')[0];
		}
		//判断是否选择了车辆或警员
		if(QueryWords == 'unselected'){
			$('#HistoryNotice').html('请根据条件'+$('#byWhat1').html());
			return false;
		}
	}else{
		var byWhat = $('#byWhat2').html() == '输入车牌'?'plate':'sn'; //判断查询警车或警员
		//获取是否车牌号或者警员编号并判断不为空
		var QueryWords = $('#querywords2').attr("value");
		if(byWhat == 'plate'){ //判断车牌不能为空
			if(QueryWords == '' || QueryWords == '豫A'){
				$('#HistoryNotice').html('请输入车牌号，可以为车辆或摩托车');
				return false;
			}
		}else{ //判断警号不能为空
			if(QueryWords == ''){
				$('#HistoryNotice').html('请输入人员员编号');
				return false;
			}
		}
		
	}
	//获取时间并判断是否符合条件
	var StartTime = $('#StartTime').attr("value");
	var EndTime   = $('#EndTime').attr("value");
	if(StartTime == ''){
		$('#HistoryNotice').html('请选择开始时间');
		return false;
	}else if(EndTime == ''){
		$('#HistoryNotice').html('请选择结束时间');
		return false;
	}else{
		var sd = new Date(StartTime.replace(/-/g,"\/"));
		var ed = new Date(EndTime.replace(/-/g,"\/"));
		if(sd>ed){
			$('#HistoryNotice').html('开始时间不能大于结束时间');
			return false;
		}
	}
	//如果所有选择的条件都符合则显示查询中……
	$('#HistoryNotice').html('系统正在查询，请稍后……'); 
	//用post方式向php传值
	xmlHttp.open('POST', 'public/Location/ajax.php', false);
	xmlHttp.onreadystatechange = QueryHistoryResult;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=QueryHistory&byWhat='+byWhat+'&QueryWords='+QueryWords+'&startime='+StartTime+'&endtime='+EndTime+'&t='+Math.random());
}

function QueryHistoryResult(){
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//获取php查询的结果字符串,并判断是否为空
			var str = xmlHttp.responseText;
			if(str == 'VehicleQueryNone'){
				$('#HistoryNotice').html('没有找到该车牌号的信息，请检查输入是否正确');
			}else if(str == 'PersonQueryNone'){
				$('#HistoryNotice').html('没有找到该人员员的信息，请检查输入是否正确');
			}else if(str == 'LocQueryNone'){
				$('#HistoryNotice').html('该时间段内没有记录');
			}else{
				//显示车辆或警员信息
				$('#HistoryNotice').html('');
				QueryInfoArr.length = 0;
				//将字符串分割成数组放到一个全局数组中
				for(var i=0;i<str.split('|').length;i++){
					var arr = str.split('|')[i].split(':');
					QueryInfoArr[arr[0]] = arr[1];
				}
				//判断显示信息
				if(QueryInfoArr['type'] == '车辆'){
					var infoStr = '车牌号: '+QueryInfoArr['plate']+'<br />所属单位: '+QueryInfoArr['departmentName']+'<br />车辆别名: '+QueryInfoArr['name']+'<br />车辆类型: '+QueryInfoArr['type']+'<br />绑定手机: '+QueryInfoArr['sim'];
				}else if(QueryInfoArr['type'] == '摩托'){
					var infoStr = '车牌号: '+QueryInfoArr['plate']+'<br />所属单位: '+QueryInfoArr['departmentName']+'<br />车辆别名: '+QueryInfoArr['name']+'<br />车辆类型: '+QueryInfoArr['type']+'<br />绑定手机: '+QueryInfoArr['sim'];
				}else{
					var infoStr = '人员姓名: '+QueryInfoArr['name']+'<br />所属单位: '+QueryInfoArr['departmentName']+'<br />人员编号: '+QueryInfoArr['sn']+'<br />人员性别: '+(QueryInfoArr['gender'] == '1'?'男':'女')+'<br />绑定手机: '+QueryInfoArr['sim'];
				}
				$('#HistoryInfo').html(infoStr);			
				JsonLocArr.length = 0; //清空历史轨迹坐标数组
				mapObj.removeAllOverlays(); //清除上次历史轨迹
				//根据坐标画出历史轨迹
				DrawHistoryLine(QueryInfoArr['loc']);
			}
		}
	}
}

//画历史轨迹线路
function DrawHistoryLine(loc){
	//将查询出来的坐标字符串转换成数组
	var locArr = loc.split('#');
	//将坐标转为[{lng:113.69548666332,lat:34.808877727405},……]的样式
	for(var i=0;i<locArr.length-1;i++){
		var arr = locArr[i].split(',');
		JsonLocArr.push({'lng':arr[0],'lat':arr[1]});
	}
	//画历史轨迹
	var PointArr = [];
	for(var i=0;i<JsonLocArr.length;i++){
		PointArr.push([JsonLocArr[i].lng, JsonLocArr[i].lat])
	}
	pantoPoint(PointArr[PointArr.length-1][0],PointArr[PointArr.length-1][1]);
	SetZoom(14);
	addMPolyline(PointArr);
	//添加节点箭头
	var rotation_jiantou = PointPointC(JsonLocArr[JsonLocArr.length-2].lng, JsonLocArr[JsonLocArr.length-2].lat, JsonLocArr[JsonLocArr.length-1].lng, JsonLocArr[JsonLocArr.length-1].lat);
	SingleMarker('HistoryLineDirection',JsonLocArr[JsonLocArr.length-1].lng, JsonLocArr[JsonLocArr.length-1].lat, false, MIDDLE_CENTER, [0,0], 'images/jiantou.png', rotation_jiantou);
	//添加起点
	SingleMarker('HistoryLineStart',JsonLocArr[0].lng, JsonLocArr[0].lat, false, MIDDLE_CENTER, [-1,-1], 'images/satrt.gif', 0);
}


/*--------------------------------------------- 警力分布 ------------------------------------------------*/
var areaArr 	  = []; //区数组
var typeArr		  = []; //警力类型
var departmentArr = []; //单位数组
var markerArr	  = []; //所有在线车辆与人员数组
function LocationBy(){	
	//结束实时轨迹的settimeout
	isClearSetTimeOut = true;
	if(RealTimeSetTimeout) clearTimeout(RealTimeSetTimeout); //清除settimeout
		if(RealTimeSetTimeout1) clearTimeout(RealTimeSetTimeout1);	
	mapObj.removeAllOverlays();	//清除轨迹及标注
	markerArr.length = 0; //添加标注前清空上次查询结果数组
	areaArr.length = 0; //添加下拉框选项前先清空数组
	SetMapCenter(OnloadCenterLng, OnloadCenterLat);
	SetZoom(OnloadZoom);	
	SetMapCenter(OnloadCenterLng, OnloadCenterLat);
	var temp='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">按照区、单位、车辆类型定位：</td></tr><tr><td><div style="position:relative;">选择条件：';
	
	
	
	temp+='<select id="area" style="width:60px" onchange="UnitOfArea(this.value);this.nextSibling.value=this.value"><option value="选择区">选择区</option></select><input type="text" value="选择区" id="input_1" onKeyUp="text_keyup_2(\'div_1\',this)" style="z-index:10000;width:42px;position:absolute;top:3px;left:60px;"><div style="display:block; top:28px; left:60px;position:absolute; z-index:10000;background:#06F;" id="div_1"></div>';

	temp+='<select id="units"  onchange="this.nextSibling.value=this.value"  style="width:100px"><option value="选择单位">选择单位</option></select><input type="text" value="选择单位" id="input_2" onKeyUp="text_keyup_3(\'div_2\',this)" style="z-index:10000;width:86px;position:absolute;top:3px;left:119px;"><div style="display:block; top:28px; left:118px;position:absolute; z-index:10000;background:#06F;" id="div_2"></div>';
	
	
	temp+='<select id="type" style="width:60px"><option value="所有类型">所有类型</option></select></div>';
	temp+='</td></tr><tr><td align="right"><input name="" type="button" class="btnbg" value="显示信息" style="width:70px;" onclick="markerBy()"/><input type="hidden" id="CallKey" /></td></tr><tr><td align="left"><div id="OnlineList"></div><div id="OnlineMsm" style="display:none;margin-top:10px"><textarea id="msgContent" style="width:100%; height:50px"></textarea><br /><input name="" type="button" class="btnbg" value="发送短信" style="width:70px;" onclick="javascript:selectedPhone(\'phone\');onlineSendMessage()"/> <input name="" id="Call" type="button" class="btnbg" value="语音通话" style="width:70px;" onclick="javascript:selectedPhone(\'phone\');MakeSomeCalls()"/> <span id="CallError" style="color:red"></span></div></td></tr></table>';
	showMapMenu("车辆分布",temp);
	$('#OnlineList').html('<font color="red">系统正在初始化查询，由于数据量可能比较大，请稍后……</font>');
	
	//利用ajax遍历选择区，单位，警力类型
	if(!xmlHttp){
		alert('不能创建XMLHttpRequest对象实例');
		return false;
	}
	xmlHttp.open('POST', 'public/Location/ajax.php', false);
	xmlHttp.onreadystatechange = QueryTerm;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=QueryTerm&t='+Math.random());
}

/*-----------------------------------------------------------------------------------------------------------------------------------------------------*/



function text_keyup_3(div,obj){
if(obj.previousSibling.options.length==1){
	obj.value="选择单位";
	return 0;
	}
if(obj.value==""){
		return 0;
		}

var n=0;
	for(var i=1; i<document.getElementById('units').options.length;i++){
	   if(document.getElementById('units').options[i].text==obj.value){
		   document.getElementById('units').options[i].selected=true;
		   n=1;
		   }
	}
	if(n==1){
		return 0;
		}

	$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"a="+document.getElementById('area').value+"&text="+obj.value,
			success:function(data){
		         if(data!=""&&data.split("_").length>0)
			     {
				 out_list(data);
				 }
				 else{
				document.getElementById(div).innerHTML="";
				}
			}
		});
	function out_list(value){
	var out="<table width='100px' style\"border-width:1px; border-color:#4c62ff; border-style:solid; \">";
	out+="<tr><td style=\"height:0px\"></td></tr>";
	for(var i=0;i<value.split("_").length;++i){
	out=out+"<tr><td onclick=\"sub_a(this.title,'input_2','div_2')\" title='"+value.split("_")[i]+"' style=\"cursor:pointer;\" onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\">"+value.split("_")[i]+"</td></tr>";
	}
	out=out+"<tr><td onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\" style=\"text-align:right;cursor:pointer;\" onclick=\"close_div('div_2')\">关闭</td></tr>";
	out=out+"</table>";
	obj1=document.getElementById("div_2");
	obj1.innerHTML=out;
		}
		
	}
function text_keyup_2(div,obj){
	var n=0;
	for(var i=1; i<document.getElementById('area').options.length;i++){
	   if(document.getElementById('area').options[i].text==obj.value){
		   document.getElementById('area').options[i].selected=true;
		     UnitOfArea(obj.value);
		   n=1;
		   }
	}
	if(n==1){
		return 0;
		}
	if(obj.value==""){
		return 0;
		}
			$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"text="+obj.value,
			success:function(data){
		         if(data!=""&&data.split("_").length>0)
			     {
				 out_list(data);
				 }
				 else{
				document.getElementById(div).innerHTML="";
				}
			}
		});
	

	function out_list(value){
		var out="<table width='60px' style\"border-width:1px; border-color:#4c62ff; border-style:solid; \">";
		out+="<tr><td style=\"height:0px\"></td></tr>";
	for(var i=0;i<value.split("_").length;++i){
	out=out+"<tr><td onclick=\"sub_a(this.title,'input_1','div_1')\" title='"+value.split("_")[i]+"' style=\"cursor:pointer;\" onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\">"+value.split("_")[i]+"</td></tr>";
	}
	out=out+"<tr><td onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\" style=\"text-align:right;cursor:pointer;\" onclick=\"close_div('div_1')\">关闭</td></tr>";
	out=out+"</table>";
	obj1=document.getElementById("div_1");
	obj1.innerHTML=out;
		}
	//document.getElementById(div).innerHTML="sdfsdf";
	}
	
function sub_a(text,a,b){
	document.getElementById(a).value=text;
		if(b=='div_2'){
	for(var i=1; i<document.getElementById('units').options.length;i++){
	   if(document.getElementById('units').options[i].text==text){
		   document.getElementById('units').options[i].selected=true;
		   }
	}
		}else{
			for(var i=1; i<document.getElementById('area').options.length;i++){
	   if(document.getElementById('area').options[i].text==text){
		   document.getElementById('area').options[i].selected=true;
		   UnitOfArea(text);
		   }
	}
	}
	close_div(b);
	}





















/*-----------------------------------------------------------------------------------------------------------------------------------------------------*/
function QueryTerm(){
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//获取php查询的结果字符串,并判断是否为空
			var str = xmlHttp.responseText;
			//alert(str);
			//return false;
			var arr = str.split('*');
			var areaStrArr = arr[0].split('|');
			for(var i=0;i<areaStrArr.length-1;i++){
				//将区写入数组
				var StrArr = areaStrArr[i].split('#');
				areaArr.push(StrArr[0]);
				//单位数组
				var unitsArr = StrArr[1].split(',');
				unitsArr.length = unitsArr.length-1;
				departmentArr.push({'area':StrArr[0], 'units':unitsArr, center:[StrArr[2], StrArr[3]]});
			}
			//将警力类型写入数组
			typeArr = arr[1].split(',');
			typeArr.length = typeArr.length-1; //将最后一个空的去掉
			//将在线人员写入数组
			var p_arr = arr[2].split('@');
			markerArr.length=0;
			for(var i=0;i<p_arr.length-1;i++){
				var pp_arr = p_arr[i].split(',');
				markerArr.push({lng:pp_arr[4], lat:pp_arr[5], type:"人员", area:pp_arr[6],units:pp_arr[3],mannum:pp_arr[2],name:pp_arr[0],device:pp_arr[7],con:'<div style="padding:5px 10px; line-height:180%">坐标：'+pp_arr[4]+","+pp_arr[5]+'<br />类型：人员<br />分区：'+pp_arr[6]+'<br />人号：'+pp_arr[2]+'<br />单位：'+pp_arr[3]+'</div>',sim:pp_arr[8]});
			}
			//将在线车辆写入数组
			var v_arr = arr[3].split('@');
			for(var i=0;i<v_arr.length-1;i++){
				var vv_arr = v_arr[i].split(',');
				markerArr.push({lng:vv_arr[4], lat:vv_arr[5], type:vv_arr[1], area:vv_arr[6],units:vv_arr[3],carsnum:vv_arr[2],device:vv_arr[7],con:'<div style="padding:5px 10px; line-height:180%">坐标：'+vv_arr[4]+","+vv_arr[5]+'<br />类型：'+vv_arr[1]+'<br />分区：'+vv_arr[6]+'<br />车牌：'+vv_arr[2]+'<br />单位：'+vv_arr[3]+'</div>',sim:vv_arr[8]});
			}
			//将数组遍历到弹出框的select中
			AddSelectOption(areaArr, 'area');
			AddSelectOption(typeArr, 'type');
			//添加所有在线的标注
			
			mapObj.removeAllOverlays();
			Markers(markerArr);
			//显示在线列表
			//OnlineList(markerArr);
			$('#OnlineList').html('<font color="green">检测到<b id="v_leng" style="color:blue">'+markerArr.length+'</b>个在线设备，选择上面的条件查看详细。</font>');
		  //v_ajax();
		  setTimeout("v_ajax()",5000);
		}
	}
}



































//遍历数组添加选项
function AddSelectOption(array, selectid){
	if(document.getElementById(selectid).options.length == 1){
		if(array){
			for( var i=0;i<array.length;i++){
				var oOption = document.createElement('OPTION');
				oOption.text = array[i];
				oOption.value = array[i];
				document.getElementById(selectid).options.add(oOption); 
			}
		}
	}
}


//根据不同的条件添加标注
function markerBy(){
	
	var newArray = [];
	var area = document.getElementById('area').value;
	var type = document.getElementById('type').value;
	var units = document.getElementById('units').value
	
	//判断是否选择了区和单位
	if(area == '选择区'){
		alert('请先选择区');
		return false;
	}else if(units == '选择单位'){
		alert('请先选择单位');
		return false;
	}
	
	//查询时的提示信息
	$('#OnlineList').html('<font color="red">系统正在查询，由于数据量比较大，请稍后……</font>');
	//根据提交的条件判断数组
	if(area !== '选择区' && type == '所有类型' && units == '选择单位'){
		for(var i=0; i<markerArr.length; i++){
			if(markerArr[i].area == area){
				newArray.push(markerArr[i]);
			}
		}
	}else if(area == '选择区' && type !== '所有类型' && units == '选择单位'){
		for(var i=0; i<markerArr.length; i++){
			if(markerArr[i].type == type){
				newArray.push(markerArr[i]);
			}
		}
	}else if(area !== '选择区' && type == '所有类型' && units !== '选择单位'){
		for(var i=0; i<markerArr.length; i++){
			if(markerArr[i].area == area && markerArr[i].units == units){
				newArray.push(markerArr[i]);
			}
		}
	}else if(area !== '选择区' && type !== '所有类型' && units == '选择单位'){
		for(var i=0; i<markerArr.length; i++){
			if(markerArr[i].area == area && markerArr[i].type == type){
				newArray.push(markerArr[i]);
			}
		}
	}else if(area == '选择区' && type == '所有类型' && units == '选择单位'){
		newArray = markerArr;
	}else if(area !== '选择区' && type !== '所有类型' && units !== '选择单位'){
		for(var i=0; i<markerArr.length; i++){
			if(markerArr[i].area == area && markerArr[i].units == units && markerArr[i].type == type){
				newArray.push(markerArr[i]);
			}
		}
	}
	//将地图中心点移动至该区的中心点
	pantoAreaCenter(area)
	//显示在线列表
	OnlineList(newArray);
	//根据条件添加标注
	Markers(newArray);
}

//确定显示信息后地图中心位置跳到选择的区域
function pantoAreaCenter(area){
	if(area !== '选择区'){
		for(var i=0; i<departmentArr.length; i++){
			if(departmentArr[i].area == area){
				var center = departmentArr[i].center;
			}
		}
		panto(center[0], center[1]);	
	}
}

//区与单位的联动
function UnitOfArea(area){
			document.getElementById("div_1").innerHTML="";
	document.getElementById("div_2").innerHTML="";
	//根据每个区的名字将单位写进数组
	for(var i=0; i<departmentArr.length; i++){
		if(departmentArr[i].area == area){
			var unitsArr = departmentArr[i].units;
		}
	}
	//每次选择前清空选择单位
	document.getElementById('units').options.length = 1
	AddSelectOption(unitsArr, 'units')
}

//根据当前在线的数组显示在线列表
function OnlineList(arr){
	if(arr.length<1){
		$("#OnlineMsm").css("display","none");
		$('#OnlineList').html('<font color="red">当前没有在线的车辆或人员！</font>');
		return false;
	}
	//计算在线数量
	var carNum = 0;
	var manNum = 0;
	var motoNum = 0;
	for(var i=0;i<arr.length;i++){
		if(arr[i].type == '人员'){
			manNum += 1;
		}else if(arr[i].type == '车辆'){
			carNum += 1;
		}else{
			motoNum += 1;
		}
	}
	var OnlineListHtml = '<style type="text/css">.t1 {display:inline-block; width:120px; text-align:left}.t2 {display:inline-block; width:60px; text-align:left}</style><b>当前在线列表</b> <font color="#666666">(在线车辆<font color="green"><b>'+carNum+'</b></font>辆，摩托<font color="green"><b>'+motoNum+'</b></font>辆，人员员<font color="green"><b>'+manNum+'</b></font>名)</font><br /><ul class="onlinelist">';
	for(var i=0;i<arr.length;i++){
		if(arr[i].type == '人员'){
			OnlineListHtml += '<li><span class="t1">'+(i+1)+'. <input type="checkbox" name="phone" onclick="isCall(this.id)" value="'+arr[i].sim+'" id="id'+arr[i].device+'" /> <label for="id'+arr[i].device+'">'+arr[i].name+'</label></span> <font color="#666666"><span class="t2">'+arr[i].type+'</span> '+arr[i].sim+'</font> '+arr[i].sim+'</li>';
		}else{
			OnlineListHtml += '<li><span class="t1">'+(i+1)+'. <input type="checkbox" name="phone" onclick="isCall(this.id)" value="'+arr[i].sim+'" id="id'+arr[i].device+'" /> <label for="id'+arr[i].device+'">'+arr[i].carsnum+'</label></span> <font color="#666666"><span class="t2">'+arr[i].type+'</span> '+arr[i].sim+'</font></li>';
		}
	}
	OnlineListHtml += '</ul>';
	$("#OnlineList").css('height','150px');
	$("#OnlineList").css('overflow-y','scroll');
	$('#OnlineList').html(OnlineListHtml);
	$("#OnlineMsm").css("display","block");
	
	//判断是否已经在通话中,如果是则为选中状态
	function in_array(a, v){ //js in_array()
		var i;
		for (i = 0; i < a.length; i++) {
			if (v === a[i]) return true;
		}
		return false;
	}
	
	//如果存在通话的数组就判断谁在通话
	if(selectedNum_arr){
		var v = document.getElementsByName('phone');
		for(var i=0; i<v.length;i++){
			if(in_array(selectedNum_arr, v.item(i).value)){
				v.item(i).checked = true;
			}
		}
	}
	return false;
}

//将选择的在线车辆或警员的手机号写入数组
var selectedNum_arr = [];
function selectedPhone(name){
	selectedNum_arr.length = 0;
	var v = document.getElementsByName(name);
	for(var i=0; i<v.length;i++){
		if(v.item(i).checked == true){
			if(v.item(i).value == ''){
				alert('设备 '+v.item(i).id+' 绑定的手机号，不能选择');
			}
			selectedNum_arr.push(v.item(i).value);
		}
	}
}

function MakeSomeCalls(){
	var selectedNum_str = selectedNum_arr.toString();
	var Status = document.getElementById('Call');
	if(selectedNum_str == ''){
		$('#CallError').html('没有绑定的手机号');
		return false;
	}
	if(Status.value == '语音通话'){
		$.ajax({
			type:"POST",
			url:"public/talk.php",
			cache:false,
			data:"act=convokeMeeting&phoneNo="+selectedNum_str,
			success:function(data){
				var re_arr = data.split(',');
				if(re_arr[0] == '100'){ //呼叫成功
					Status.value = '挂断通话'; //按钮变为挂断
					$('#CallError').html('<font color="green">不要挂断，直接选择加入或踢出</font>');
					document.getElementById('CallKey').value = re_arr[1]; // 将通话的唯一标示赋给赋给隐藏按钮
				}else if(re_arr[0] == '101'){
					$('#CallError').html('101 IP地址有误');
					selectedNum_arr.length = 0;
					return false;
				}else if(re_arr[0] == '102'){
					$('#CallError').html('102 其他错误');
					selectedNum_arr.length = 0;
					return false;
				}else{
					$('#CallError').html('调用webserverice错误');
					selectedNum_arr.length = 0;
					return false;
				}
			}
		});
	}else{
		var CallKey = document.getElementById('CallKey').value // 将通话的唯一标示
		$.ajax({
			type:"POST",
			url:"public/talk.php",
			cache:false,
			data:"act=endMeeting&number="+CallKey,
			success:function(data){
				if(data == '100'){ //挂断成功
					Status.value = '语音通话';
					document.getElementById('CallKey').value = ''; // 将通话的唯一标示删除
					$('#CallError').html('<font color="green">挂断成功</font>');
				}
			}
		});
	}
}

function isCall(id){
	var Status = document.getElementById('Call').value;
	if(Status == '语音通话'){
		return false;
	}
	var phonenum = document.getElementById(id).value;		//加入的号码
	var CallKey = document.getElementById('CallKey').value  //通话的唯一标示
	selectedPhone('phone');	//再次获取所有通话的电话号码
	//加入新通话
	if(document.getElementById(id).checked == true){
		if(!confirm('您确定将 '+phonenum+' 加入本次通话？')){
			document.getElementById(id).checked = false;
			return false;
		}
		//用ajax调用加入新通话
		$.ajax({
			type:"POST",
			url:"public/talk.php",
			cache:false,
			data:"act=reCall&phoneNo="+phonenum+"&number="+CallKey,
			success:function(data){
				if(data == '100'){ //加入成功
					$('#CallError').html('<font color="green">'+phonenum+' 加入成功</font>');
				}else if(data == '101'){
					$('#CallError').html('101 IP地址有误');
					return false;
				}else if(data == '102'){
					$('#CallError').html('102 其他错误');
					return false;
				}else{
					$('#CallError').html('调用webserverice错误');
					return false;
				}
			}
		});
	}
	//踢出通话
	else{
		if(!confirm('您确定将 '+phonenum+' 踢出本次通话？')){
			document.getElementById(id).checked = true;
			return false;
		}
		//用ajax调用踢出通话
		$.ajax({
			type:"POST",
			url:"public/talk.php",
			cache:false,
			data:"act=handUp&phoneNo="+phonenum+"&number="+CallKey,
			success:function(data){
				if(data == '100'){ //踢出成功
					$('#CallError').html('<font color="green">'+phonenum+' 踢出通话成功</font>');
				}else if(data == '101'){
					$('#CallError').html('101 IP地址有误');
					return false;
				}else if(data == '102'){
					$('#CallError').html('102 其他错误');
					return false;
				}else{
					$('#CallError').html('调用webserverice错误');
					return false;
				}
			}
		});
	}
}


//-------------------- 发送短信 --------------------------//

//警力分布时多个手机号发送短信
function onlineSendMessage(){
	var selectedNum_str = selectedNum_arr.toString();
	var Status = document.getElementById('Call');
	if(selectedNum_str == ''){
		$('#CallError').html('没有绑定的手机号');
		return false;
	}
	//调用发送短信的函数
	sendMessage(selectedNum_str);
}

//短信发送
function sendMessage(phoneNum){
	var msgContent = document.getElementById('msgContent').value;
	//判断短信内容是否为空
	if(msgContent == ''){
		$('#CallError').html('短信内容不能为空');
		return false;
	}else{
		$('#CallError').html('');
	}
	$.ajax({
		type:"POST",
		url:"public/msg.php",
		cache:false,
		data:"phoneNo="+phoneNum+"&msg="+msgContent,
		success:function(data){
			//alert(data);
			if(data == '1'){ //发送成功
				$('#CallError').html('<font color="green">短信发送成功！</font>');
				return false;
			}else if(data == '0'){
				$('#CallError').html('短信发送失败！');
				return false;
			}else if(data == '2'){
				$('#CallError').html('短信内容不能为空！');
				return false;
			}else if(data == '4'){
				$('#CallError').html('调用webserverice错误！');
				return false;
			}else{
				$('#CallError').html('发生未知错误！');
				return false;
			}
		}
	});
}


//-------------------- 选择警员，警车 --------------------------//

//选择查询方式
function QueryMethod(method){
	//清空已经选择的时间
	$('#StartTime').val('');
	$('#EndTime').val('');
	//实时轨迹时 清除settimeout
		if(RealTimeSetTimeout1) clearTimeout(RealTimeSetTimeout1);	
	if(RealTimeSetTimeout){
		clearTimeout(RealTimeSetTimeout);
		mapObj.removeAllOverlays(); //清除轨迹及标注
		$('#RealTimeInfo').html(''); //切换查询方式后 清除已经查询到的信息
		$('#RealTimeNotice').html(''); //切换查询方式后 清除已经查询到的信息
		$("#RealtimeMsm").css("display","none"); //切换查询方式后 隐藏打电话发短信
	}
	if(method == '按条件查'){
		document.getElementById('method1').style.display = 'block';
		document.getElementById('method2').style.display = 'none';
	}else{
		document.getElementById('method1').style.display = 'none';
		document.getElementById('method2').style.display = 'block';
	}
}

//显示选择条件，区、派出所、类型
function showSelect(){
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var str = xmlHttp.responseText;
			jsonObj = eval(str);
			//将区写入下拉框
			if(document.getElementById('area').options.length = 1){
				for( var i=0;i<jsonObj.length;i++){
					var oOption = document.createElement('OPTION');
					oOption.text = jsonObj[i].AREA;
					oOption.value = jsonObj[i].AREA;
					document.getElementById('area').options.add(oOption); 
				}
			}
		}
	}
}

//根据分区联动单位
function unitsOption(area){
	document.getElementById("div_1").innerHTML="";
	document.getElementById("div_2").innerHTML="";
	if(area == '选择区'){
		document.getElementById('units').length = 1;
		document.getElementById('querywords1').length = 1;
		document.getElementById('querywords1').disabled = true;
		return false;
	}
	document.getElementById('querywords1').length = 1;
	//将对应的单位写入下拉框
	for(var i=0; i<jsonObj.length;i++){
		if(jsonObj[i].AREA == area){
			if(document.getElementById('units').options.length = 1){
				var unitsObj = jsonObj[i].INFO
				for( var j=0;j<unitsObj.length;j++){
					var oOption = document.createElement('OPTION');
					oOption.text = unitsObj[j].DEPARTMENT;
					oOption.value = unitsObj[j].DEPARTMENT;
					document.getElementById('units').options.add(oOption); 
				}
			}
		}
	}
}

//根据单位联动所属车辆及警员
function querywordsOption(units){
		document.getElementById("div_1").innerHTML="";
	document.getElementById("div_2").innerHTML="";
	if(units == '选择单位'){
		document.getElementById('querywords1').length = 1;
		document.getElementById('querywords1').disabled = true;
		return false;
	}
	//如果选择了单位，默认警力类型为警车，所以选择车辆变为可选状态
	document.getElementById('querywords1').disabled = false;
	//根据默认选项加载 车牌、警员 的选项
	var type = document.getElementById('type').value;
	var area = document.getElementById('area').value;
	typeOption(type)
}

//根据警力类型联动车辆与警员
function typeOption(type){
	//添加option之前先清空
	document.getElementById('querywords1').length = 1;
	var units = document.getElementById('units').value;
	var area = document.getElementById('area').value;
	//根据选择警力类型的不同，提示不同的选择信息
	if(type == '车辆'){
		$('#byWhat1').html('选择车牌');
	}else if(type == '摩托'){
		$('#byWhat1').html('选择车牌');
	}else{
		$('#byWhat1').html('选择人员号');
	}
	//加载警力选项
	for(var i=0;i<jsonObj.length;i++){
		if(jsonObj[i].AREA == area){
			var infoObj = jsonObj[i].INFO;
			for(var j=0;j<infoObj.length;j++){
				//根据已经选择的警力类型 判断显示的值
				if(infoObj[j].DEPARTMENT == units){
					if(type == '车辆'){
						var Obj = infoObj[j].CAR;
					}else if(type == '摩托'){
						var Obj = infoObj[j].MOTO;
					}else{
						var Obj = infoObj[j].MAN;
					}
					//如果为空，则提示该单位下没有+type
					if(Obj.length <= 0){
						document.getElementById('querywords1').options[0] = new Option('没有符合条件的'+type,'unselected');
						return false;
					}
					//如果有值 根据警力类型变换选择提示 “选择车辆、选择摩托、选择警员”
					document.getElementById('querywords1').options[0] = new Option('选择'+type,'unselected');
					//添加选项
					for( var k=0;k<Obj.length;k++){
						var oOption = document.createElement('OPTION');
						oOption.text = Obj[k];
						oOption.value = Obj[k];
						document.getElementById('querywords1').options.add(oOption); 
					}
					break;
				}
			}
			break;
		}
	}
}



//-----------------------------------------实时监控---------------------------------------------
function timing(){
	//charnum[1]!=""
	if(charnum.length!=0){
		//结束实时轨迹的settimeout
	isClearSetTimeOut = true;
	if(RealTimeSetTimeout) clearTimeout(RealTimeSetTimeout);	//清除settimeout
		if(RealTimeSetTimeout1) clearTimeout(RealTimeSetTimeout1);	
	mapObj.removeAllOverlays();			//清除轨迹及标注
	//弹窗
	var temp='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">查询指定人员或车辆的当前位置并实时监控：</td></tr><tr><td>查询条件：<input type="radio" id="ByPlatenum" name="By" value="车牌" onclick="RealTimeBy(\'输入车牌\')" checked="checked" /> 车牌号<input type="radio" id="ByPlatenum" style="display:none" name="By"/></td></tr><tr><td><label id="byWhat2">输入车牌</label>：<input type="text" disabled="disabled" id="querywords2" name="querywords" value="" style="width:180px; color:#666" /></td></tr><tr><td align="right"><input name="" type="button" class="btnbg" value="实时监控" style="width:70px;" onclick="javascript:QueryInfoAjax()"/><input name="" type="button" class="btnbg" value="停止监控" style="width:70px;" onclick="javascript:QueryInfoStop()"/><input type="hidden" id="CallKey" /></td></tr><tr><td align="left"><div id="RealTimeNotice" style="color:red"></div><div id="RealTimeInfo"></div><div id="RealtimeMsm" style="display:none"><textarea id="msgContent" style="width:100%; height:50px"></textarea><br /><input name="" type="button" class="btnbg" value="发送短信" style="width:70px;" onclick="javascript:sendMessage(document.getElementById(\'phonenum\').innerHTML)"/> <input name="" id="Call" type="button" class="btnbg" value="语音通话" style="width:70px;" onclick="javascript:realtimeCall()"/> <span id="CallError" style="color:red"></span></div></td></tr></table>';
	temp+='<div style="display:none"><input type="radio" name="method"/><input name="method" checked="checked" type="radio"/></div>';
	showMapMenu("实时监控",temp);
	$('#querywords2').val(charnum[1]);
	}else{
		var temp='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:0px solid #000;"><font color="red">当前时间段没有要定时的车辆</font></td></tr>';
	showMapMenu("实时监控",temp);
	}
}











/*--------------------------------------------------------------------------------------------------------------------------------------------*/


function v_ajax(){
	$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"v=1",
			success:function(data){
	if(parseInt(data)!=parseInt(document.getElementById("v_leng").innerHTML)){
	document.getElementById("v_leng").innerHTML=data;
	xmlHttp.open('POST', 'public/Location/ajax.php', false);
	xmlHttp.onreadystatechange = QueryTerm;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send('action=QueryTerm&t='+Math.random());		 
					 }
			     RealTimeSetTimeout1=setTimeout("v_ajax()",10000);
			}
		});
	}



/*--------------------------------------------------------------------------------------------------------------------------------------------*/